package com.echo.im.client.status;


import cn.hutool.core.collection.CollUtil;
import com.echo.im.client.model.IMTerminalType;
import com.echo.im.client.model.IMUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author echo
 */
@Slf4j
public class UserStatusServiceImpl implements UserStatusService {

    private final UserStatusRepository userStatusRepository;

    public UserStatusServiceImpl(UserStatusRepository userStatusRepository) {
        this.userStatusRepository = userStatusRepository;
    }

    @Override
    public Map<Integer, List<Integer>> getServerIdTerminalListMap(Long userId, List<Integer> terminals) {
        Map<Integer, List<Integer>> serverIdMap = new HashMap<>();
        Map<Integer, Integer> terminalStatus = userStatusRepository.findTerminalServerIdMap(userId);
        for (Integer terminal : terminals) {
            Integer serverId = terminalStatus.get(terminal);
            if (serverId != null) {
                serverIdMap.computeIfAbsent(serverId, k -> new ArrayList<>()).add(terminal);
            }
        }
        return serverIdMap;
    }

    @Override
    public Map<Integer, List<IMUserInfo>> getServerIdUserInfoListMap(List<Long> userIds) {
        return getServerIdUserInfoListMapWithOffline(userIds, null);
    }

    @Override
    public Map<Long, List<IMTerminalType>> getUserIdTerminalListMap(List<Long> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return Collections.emptyMap();
        }

        Map<Long, Map<Integer, Integer>> userTerminalStatusMap = userStatusRepository.findUserTerminalServerIdMap(userIds);
        Map<Long, List<IMTerminalType>> onlineMap = new HashMap<>();

        for (Map.Entry<Long, Map<Integer, Integer>> entry : userTerminalStatusMap.entrySet()) {
            Long userId = entry.getKey();
            List<IMTerminalType> terminals = new LinkedList<>();

            for (Integer terminal : entry.getValue().keySet()) {
                terminals.add(IMTerminalType.fromCode(terminal));
            }

            if (!terminals.isEmpty()) {
                onlineMap.put(userId, terminals);
            }
        }
        return onlineMap;
    }

    @Override
    public Boolean isUserOnline(Long userId) {
        Map<Integer, Integer> terminalStatus = userStatusRepository.findTerminalServerIdMap(userId);
        return !terminalStatus.isEmpty();
    }

    @Override
    public List<Long> getOnlineUserIdList(List<Long> userIds) {
        return new ArrayList<>(getUserIdTerminalListMap(userIds).keySet());
    }

    @Override
    public Map<Integer, List<IMUserInfo>> getServerIdUserInfoListMapWithOffline(List<Long> userIds, Set<Long> offlineUserIdList) {
        if (CollUtil.isEmpty(userIds)) {
            return Collections.emptyMap();
        }

        Map<Long, Map<Integer, Integer>> userTerminalServerIdMap = userStatusRepository.findUserTerminalServerIdMap(userIds);

        // 确保 offlineUserIdList 不为空
        if (offlineUserIdList != null) {
            // 修正逻辑：找出没有终端信息的用户作为离线用户
            Set<Long> userIdsWithoutTerminals = new HashSet<>(userIds);
            userIdsWithoutTerminals.removeAll(userTerminalServerIdMap.keySet());
            offlineUserIdList.addAll(userIdsWithoutTerminals);
        }

        Map<Integer, List<IMUserInfo>> serverMap = new HashMap<>();

        for (Map.Entry<Long, Map<Integer, Integer>> entry : userTerminalServerIdMap.entrySet()) {
            Long userId = entry.getKey();
            Map<Integer, Integer> terminalStatusMap = entry.getValue();

            for (Map.Entry<Integer, Integer> terminalEntry : terminalStatusMap.entrySet()) {

                Integer terminalType = terminalEntry.getKey();

                Integer serverId = terminalEntry.getValue();
                IMUserInfo userInfo = new IMUserInfo(userId, terminalType);
                serverMap.computeIfAbsent(serverId, k -> new LinkedList<>()).add(userInfo);
            }

        }
        return serverMap;
    }
}
